package com.fastjrun.ddns.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fastjrun.ddns.dao.DdnsRecordDao;
import com.fastjrun.ddns.dto.DdnsRecordDTO;
import com.fastjrun.ddns.entity.DdnsRecord;
import com.fastjrun.ddns.helper.DTOToEntityConverter;
import com.fastjrun.ddns.helper.EntityToVOConverter;
import com.fastjrun.ddns.service.RecordService;
import com.fastjrun.ddns.vo.DdnsRecordVO;
import com.ruoyi.common.core.page.TableDataInfo;

import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service("recordService")
public class RecordServiceImpl implements RecordService {
    @Resource
    DdnsRecordDao ddnsRecordDao;

    @Override
    public List<DdnsRecordVO> list() {
        List<DdnsRecordVO> ddnsRecordVOS = ddnsRecordDao.list().stream().map(EntityToVOConverter::convert
        ).collect(Collectors.toList());
        return ddnsRecordVOS;
    }

    @Override
    public void add(DdnsRecordDTO ddnsRecordDTO) {
        DdnsRecord ddnsRecord = DTOToEntityConverter.convert(ddnsRecordDTO);
        Date date=new Date();
        ddnsRecord.setCreateDate(date);
        ddnsRecord.setUpdateDate(date);
        ddnsRecordDao.save(ddnsRecord);
    }

    @Override
    public void deleteById(String id) {
        ddnsRecordDao.removeById(id);

    }

    @Override
    public TableDataInfo<DdnsRecordVO> page(Integer pageNum, Integer pageSize) {
        Page<DdnsRecord> page = ddnsRecordDao.selectJoinListPage(new Page<>(pageNum,pageSize), DdnsRecord.class, null);

        TableDataInfo<DdnsRecordVO> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(page.getRecords().stream().map(EntityToVOConverter::convert).collect(Collectors.toList()));
        return tableDataInfo;
    }
}
